import Mock from "mockjs";
import dayjs from "dayjs";
import querystring from 'querystring'
import { songFun } from "@/utils/index";

// phone
Mock.Random.extend({
    phone() {
        const start = ['138', '139', '151', '158', '187']
        return this.pick(start) + Mock.mock(/\d{8}/)
    }
})
// 对表格数据分页
const getSelectList = (data, pageNum, pageSize) => {
    console.log('getSelectList==', data);
    let kshi = Math.ceil(((pageNum - 1) * pageSize).toFixed(0))
    let jshu = Math.ceil((kshi + pageSize).toFixed(2))
    const list = data.slice(kshi >= 0 ? kshi : 0, jshu)
    return list
}
// 统一时间格式
const gsTime = function (time) {
    return dayjs(time).format('YYYY-MM-DD')
}
// 列表数据
const list = Mock.mock({
    'datas|15': [
        {
            'id|+1': 1010,
            'Bianhao': '@integer(10000000000000,999999999999999999)',
            'dueTime': '@datetime("2024-02-dd hh:mm:ss")',
            'placePerson': '@cname',
            'unit': '@ctitle(5,10)',
            'phoneNumber': '@phone',
            'totalValue': '@integer(100,999)',
            'state': '@pick([0,1])'      // 0 未汇总  1 已汇总     
        }
    ]
})
// 汇总订单页 列表数据
let hzData = Mock.mock({
    'datas': [
        // {
        //     'id|+1': 1002,
        //     'hzBianhao': '@integer(100000,9999999)',
        //     'hzPerson': '@cname',
        //     'phoneNumber': "@phone",
        //     'hzTime': '@datetime("2024-02-dd HH:mm:ss")',
        //     'totalPrice': '@integer(1000,9999)'
        // }
    ]
})
let zhAfterDatas = hzData.datas
let afterData = list.datas
/** 
 * 按钮查询 和 页面加载用的是同一个接口
 * 必传 pageNumber pageSize 
 * 非必传 Bianhao: '',bingTime: '',endTimg: '',placePerson: '',unit: '',state: '' 
 * **/
Mock.mock(/\/mock\/order-list/, 'get', ({ url }) => {
    let obj = querystring.parse(url.split('?')[1])
    const { pageNumber, pageSize, state, ...newObj } = obj;   // newObj 是去除了 pageNumber pageSize 两个属性之后的 新对象
    console.log(pageNumber, pageSize, state);
    if (obj.state == '2' && songFun.isEmpty(newObj)) {   // 页面初始加载 或是 重置以后点击查询
        let aftList = getSelectList(afterData, Number(obj.pageNumber), Number(obj.pageSize))
        return {
            code: 200,
            message: 'success',
            total: afterData.length,
            datas: aftList
        }
    } else {   // 带其他查询条件 点击查询 按钮
        let needData = []
        needData = afterData.filter(val => val.Bianhao == obj.Bianhao || gsTime(val.dueTime) == gsTime(obj.bingTime) || gsTime(val.dueTime) == gsTime(obj.endTimg) || val.placePerson == obj.placePerson || val.unit == obj.unit || val.state == obj.state)
        let aftList = getSelectList(needData, Number(obj.pageNumber), Number(obj.pageSize))
        return {
            code: 200,
            message: 'success',
            total: needData.length,
            datas: aftList
        }
    }
})
// 汇总订单 按钮
Mock.mock(/\/mock\/huizong/, 'post', ({ body }) => {
    let idArr = JSON.parse(body).ids.split(',')
    let total = 0
    // 修改当前页的数据状态
    idArr.forEach(item => {
        afterData.forEach((jk, index) => {
            if (jk.id == item) {
                afterData[index].state = 1
                total = total + Number(afterData[index].totalValue)
            }
        })
    })
    // 增加汇总订单页 汇总记录
    let hzObj = {
        'id': Math.ceil(Math.random() * 1000),
        'hzBianhao': Math.ceil(Math.random() * 1000),
        'hzPerson': 'xiuyi',
        'phoneNumber': "18746520236",
        'hzTime': dayjs().format('YYYY-MM-DD HH:mm:ss'),
        'totalPrice': total,
        'idArr': idArr
    }
    zhAfterDatas.unshift(hzObj)
    return {
        code: 200,
        message: 'success'
    }
})
// 汇总订单页 列表数据
Mock.mock(/\/mock\/getHZList/, 'get', ({ url }) => {
    let obj = querystring.parse(url.split('?')[1])
    const { pageNumber, pageSize, state, ...newObj } = obj;   // newObj 是去除了 pageNumber pageSize 两个属性之后的 新对象
    console.log(pageNumber, pageSize, state);
    if (songFun.isEmpty(newObj)) {   // 页面初始加载 或是 重置以后点击查询
        let aftList = getSelectList(zhAfterDatas, Number(obj.pageNumber), Number(obj.pageSize))
        return {
            code: 200,
            message: 'success',
            total: zhAfterDatas.length,
            datas: aftList
        }
    } else {   // 带其他查询条件 点击查询 按钮
        let needData = []
        needData = zhAfterDatas.filter(val => val.hzBianhao == obj.hzBianhao || gsTime(val.hzTime) == gsTime(obj.bingTime) || gsTime(val.hzTime) == gsTime(obj.endTimg) || val.hzPerson == obj.hzPerson || val.totalPrice == obj.totalPrice || val.phoneNumber == obj.phoneNumber)
        let aftList = getSelectList(needData, Number(obj.pageNumber), Number(obj.pageSize))
        return {
            code: 200,
            message: 'success',
            total: needData.length,
            datas: aftList
        }
    }
})
// 撤销汇总
Mock.mock(/\/mock\/hzRevoke/, 'post', ({ body }) => {
    // 还原列表页的数据状态
    let idArr = JSON.parse(body).row.idArr
    idArr.forEach(item => {
        afterData.forEach((jk, index) => {
            if (jk.id == item) {
                afterData[index].state = 0
            }
        })
    })
    // 删除当前页的汇总记录
    let revokId = JSON.parse(body).row.id
    let revokIndex = zhAfterDatas.findIndex(item => item.id == revokId)
    zhAfterDatas.splice(revokIndex, 1)
    return {
        code: 200,
        message: 'success'
    }
})
//详情页表格
const detaiData = Mock.mock({
    'datas|6':[
        {
            'goodsName':'@ctitle(3)',
            'price':'10',
            'guige':'500G',
            'number':'10',
            'smallComput':'100'
        }
    ]
})
Mock.mock(/\/mock\/detail-list/,'get',({url})=>{
    console.log('url',url);
    let obj = querystring.parse(url.split("?")[1])
    console.log('obj',obj);
    if(!obj.ids) return alert("must click detail")
    return {
        code: 200,
        message:'success',
        footer:{'shState':1 , 'shPerson':'xiuyi', 'shTime':dayjs().format("YYYY-MM-DD HH:mm:ss"), 'shJanyi':'is good idea'},
        ...detaiData
    }
})
